# -*- coding:utf-8 -*-

from django.contrib import admin
from account.models import *
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin

def init_management_team():
    try:
        management_team = Group.objects.get(name='management-team')
    except Group.DoesNotExist:
        management_team = Group(name="management-team")
        management_team.save()
    return management_team

class FilterWechatLogAdmin(admin.ModelAdmin):
    def queryset(self,request):
        qs = super(FilterWechatLogAdmin, self).queryset(request)
        if request.user.is_superuser:
            return qs.order_by('-created_at')
        if request.user.groups.filter(name=init_management_team().name).count() > 0:
            return qs.filter(Q(customer__category='1')|Q(customer__category='2')|Q(customer__category='3')).order_by('-created_at')
        return qs.filter(Q(customer__owner=request.user)|Q(customer__assistants__icontains=request.user.username)|Q(customer__owner__isnull=True)).order_by('-created_at')

class UserShareFollowAdminInline(admin.StackedInline):
    # readonly_fields = ('share','user','created_at','modify_at')
    model = UserShareFollow
    raw_id_fields = ('user',)
    readonly_fields = ('user','modify_at')
    ordering = ('-create_at',)
    extra = 0

class WechatLogAdminInline(admin.StackedInline):
    readonly_fields = ('user','msg_type','content','created_at')
    model = WechatLog
    ordering = ('-created_at',)
    extra = 0

@admin.register(WechatLog)
class WechatLogAdmin(FilterWechatLogAdmin):
    date_hierarchy = 'created_at'
    readonly_fields = ('msg_type','content','created_at')
    list_display = ('created_at','user','msg_type')
    list_filter = ('user','msg_type')

@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
    # date_hierarchy = 'date_joined'
    search_fields = ['user__username','truename','nickname','user__email','school','phone','addr','qq','city','openid']
    list_display = ('userid','truename','nickname','gender','city','phone','subscribed','mobile_verified')
    list_filter = ('category','city','gender','stars','subscribed','mobile_verified','email_verified')
    inlines = [WechatLogAdminInline]

    def format_date(obj):
        return obj.date.strftime('%YYYY-%m-%d')
    format_date.short_description = 'Date'

@admin.register(WxConfig)
class WxConfigAdmin(admin.ModelAdmin):
    date_hierarchy = 'datetime_created'
    search_fields = ['key','value','remark']
    list_display = ('key','category','datetime_created','status')
    list_filter = ('category','status')

@admin.register(Item)
class ItemAdmin(admin.ModelAdmin):
    date_hierarchy = 'create_at'
    search_fields = ['id','article_id','article_title']
    # list_display = ('article_id','list_link_item','item_title_summary','article_price','active','special','top_category','crawl_cnt','error','create_at','modify_at')
    list_display = ('item_title_summary','list_link_item','rmb_price','active','special','top_category','error','create_at')
    list_filter = ('active','expired','special','article_from','error','top_category','article_mall')
    readonly_fields = ('link_pc_item', 'link_mobile_item', 'create_at','modify_at')
    raw_id_fields = ('article_mall','brand')

@admin.register(Brand)
class BrandAdmin(admin.ModelAdmin):
    date_hierarchy = 'create_at'
    search_fields = ['name','country','remarks']
    list_display = ('name','country','active','create_at','modify_at')
    list_filter = ('active',)
    readonly_fields = ('create_at','modify_at')

@admin.register(Mall)
class MallAdmin(admin.ModelAdmin):
    date_hierarchy = 'create_at'
    search_fields = ['name','code','pinyin','mall_url','mall_url_mb','remarks']
    list_display = ('name','active','code','pinyin','plan_code','active','order','create_at','modify_at')
    list_filter = ('active',)
    readonly_fields = ('create_at','modify_at')

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
    date_hierarchy = 'create_at'
    search_fields = ['name','code','remarks']
    list_display = ('name','code','parent','active','order','create_at','modify_at')
    list_filter = ('active',)
    readonly_fields = ('create_at','modify_at')

@admin.register(Channel)
class ChannelAdmin(admin.ModelAdmin):
    date_hierarchy = 'create_at'
    search_fields = ['name','code','remarks']
    list_display = ('name','code','parent','active','order','create_at','modify_at')
    list_filter = ('active',)
    readonly_fields = ('create_at','modify_at')

@admin.register(UserShareFollow)
class UserShareFollowAdmin(admin.ModelAdmin):
    date_hierarchy = 'create_at'
    list_display = ('share', 'user', 'create_at', 'modify_at')
    readonly_fields = ('share', 'user','create_at','modify_at')

@admin.register(UserShare)
class UserShareAdmin(admin.ModelAdmin):
    date_hierarchy = 'create_at'
    raw_id_fields = ('goods_item','owner')
    list_display = ('share_type', 'goods_item', 'owner' ,'qrcode_type', 'active', 'qrcode_expire_at')
    list_filter = ('active', 'qrcode_type', 'share_type')
    readonly_fields = ('link_item','qrcode_expire_at','display_qrcode','create_at','modify_at')
    inlines = [UserShareFollowAdminInline]


@admin.register(Keyword)
class KeywordAdmin(admin.ModelAdmin):
    date_hierarchy = 'create_at'
    search_fields = ['name','remarks']
    list_display = ('name', 'subscribe_times', 'create_at', 'modify_at')
    readonly_fields = ('create_at','modify_at')

@admin.register(UserVsKeyword)
class UserVsKeywordAdmin(admin.ModelAdmin):
    date_hierarchy = 'create_at'
    search_fields = ['keyword__name', 'keyword__remarks', 'include', 'exclude']
    list_display = ('keyword', 'user', 'active', 'include', 'exclude', 'notify_times', 'expire_at', 'create_at')
    list_filter = ('active','user')
    readonly_fields = ('create_at','modify_at')

@admin.register(Star)
class StarAdmin(admin.ModelAdmin):
    date_hierarchy = 'create_at'
    search_fields = ['name','name_pinyin']
    list_display = ('name','gender','nationality','birth_date','constellation','height','score')
    list_filter = ('gender','constellation',)
    readonly_fields = ('create_at','modify_at')
